import numpy as np
from Utilities.AutoGetOperator.selectPackage import get_func

AHP=get_func(r'DecisionMethod/AHP.py','AHP')
'''
用于元素之间相互影响
'''


class ANP:
    def __init__(self, matrix_1, matrix_2, q):
        self.elementMatrix = matrix_1  # [[[[prm],[prm]], [],[]], [], []  ]
        self.clusterMatrix = matrix_2  # [[prm], [], []]
        self.q = q

    def getWeightVectorMatrix(self, matrix, q):
        """

        :param matrix: 广义正交矩阵
        :param q: q值
        :return: 权重向量
        """

        ahp = AHP(matrix, q)
        return ahp.getResult()
        pass

    def getElementWeightMatrix(self, matrix, q):
        """

        :param matrix: 元素群的广义正交矩阵集
        :param q:
        :return: 元素群权重向量集
        """
        elementWeightMatrix = []
        for i in range(len(matrix)):  # cluster i  c1, c2
            clusterWeight = []
            for j in range(len(matrix[0])):  # cluster i preference cluster j
                W = []
                for k in range(len(matrix[i][j])):  # element of cluster i preference element k of cluster
                    w = self.getWeightVectorMatrix(matrix[i][j][k], q)
                   # print('C{:} preference e{:} of C{:}'.format(i + 1, k + 1, j + 1))
                    W.append(w)
                #    W = np.transpose(W)  # 转置
                W = np.transpose(W).tolist()  # 转置
                clusterWeight.append(W)
            elementWeightMatrix.append(clusterWeight)
        return elementWeightMatrix

    def getClusterWeightMatrix(self, matrix, q):
        """

        :param matrix: 簇群广义正交矩阵集
        :param q:
        :return: 簇群权重向量集
        """
        clusterWeight = []
        for i in range(len(matrix)):  # clusters preference cluster i
            w = self.getWeightVectorMatrix(matrix[i], q)
            clusterWeight.append(w)
        clusterWeight = np.transpose(clusterWeight)
        return clusterWeight

    def getSuperMatrix(self, matrix1, matrix2):
        """

        :param matrix1: 元素群权重向量集
        :param matrix2: 簇群权重向量集
        :return: 超矩阵集
        """
        def multiplicationMatrix(matrix, num):
            for i in range(len(matrix)):
                for j in range(len(matrix[0])):
                    matrix[i][j] = matrix[i][j] * num
            return matrix

        superMatrix = []
        n = len(matrix1)
        m = len(matrix1[0])
        for i in range(n):
            s = []
            for j in range(m):
                a = matrix1[i][j]
                b = matrix2[i][j]
                matrix = multiplicationMatrix(a, b)
                s.append(matrix)
            superMatrix.append(s)
        return superMatrix

    def aggregateSuperMatrix(self, superMatrix):
        """

        :param superMatrix: 超级矩阵集
        :return: 超矩阵集融合成超矩阵
        """
        aggregatedSuperMatrix = []
        for i in range(len(superMatrix)):
            for k in range(len(superMatrix[i][0])):
                a = []
                for j in range(len(superMatrix[i])):
                    a += superMatrix[i][j][k]
                aggregatedSuperMatrix.append(a)
        return aggregatedSuperMatrix

    def limitedMatrix(self, matrix):
        """
        :param matrix:超矩阵
        :return:权重向量
        """
        # 条件过于苛刻，不一定能得出结果，待更好的方法
        def testing(a):
            for k in range(len(a)):
                for j in range(len(a[0]) - 1):
                    if a[k][j] != a[k][j + 1]:
                        return True

        m = matrix
        n = 0
        while testing(matrix):
            matrix = np.dot(np.array(matrix), np.array(m))
            n += 1
            if n >= 30:
                break
        weight = []
        for i in range(len(matrix)):
            weight.append(matrix[i][0])
        sum = 0
        for i in range(len(weight)):
            sum += weight[i]
        for i in range(len(weight)):
            weight[i] = weight[i] / sum
        return weight

    def getResult(self):
        elementMatrix = self.getElementWeightMatrix(self.elementMatrix, self.q)
        clusterMatrix = self.getClusterWeightMatrix(self.clusterMatrix, self.q)
        superMatrix = self.getSuperMatrix(elementMatrix, clusterMatrix)
        aggregateMatrix = self.aggregateSuperMatrix(superMatrix)
        weightVector = self.limitedMatrix(aggregateMatrix)
        return weightVector


if __name__ == '__main__':
    # C1:e1, e2, e3, e4    C2: e5, e6, e7
    elementData = np.array([
        [[
            [[([0.5, 0.5], [0.5, 0.5]), ([0.5, 0.6], [0.3, 0.4]), ([0.5, 0.7], [0.2, 0.3]), ([0.5, 0.8], [0.1, 0.2])],
             [([0.3, 0.4], [0.5, 0.6]), ([0.5, 0.5], [0.5, 0.5]), ([0.6, 0.7], [0.2, 0.3]), ([0.6, 0.7], [0.1, 0.2])],
             [([0.2, 0.3], [0.5, 0.7]), ([0.2, 0.3], [0.6, 0.7]), ([0.5, 0.5], [0.5, 0.5]), ([0.6, 0.7], [0.1, 0.3])],
             [([0.1, 0.2], [0.5, 0.8]), ([0.1, 0.2], [0.6, 0.7]), ([0.1, 0.3], [0.6, 0.7]), ([0.5, 0.5], [0.5, 0.5])]],
            # prm_C1_e1
            [[([0.5, 0.5], [0.5, 0.5]), ([0.5, 0.6], [0.2, 0.3]), ([0.5, 0.7], [0.2, 0.3]), ([0.5, 0.8], [0.1, 0.2])],
             [([0.2, 0.3], [0.5, 0.6]), ([0.5, .5], [0.5, 0.5]), ([0.6, 0.7], [0.2, 0.3]), ([0.6, 0.7], [0.1, 0.2])],
             [([0.2, 0.3], [0.5, 0.7]), ([0.2, 0.3], [0.6, 0.7]), ([0.5, 0.5], [0.5, 0.5]), ([0.6, 0.7], [0.1, 0.3])],
             [([0.1, 0.2], [0.5, 0.8]), ([0.1, 0.2], [0.6, 0.7]), ([0.1, 0.3], [0.6, 0.7]), ([0.5, 0.5], [0.5, 0.5])]],
            # prm_C1_e2
            [[([0.5, 0.5], [0.5, 0.5]), ([0.6, 0.9], [0.0, 0.3]), ([0.6, 0.7], [0.2, 0.3]), ([0.6, 0.7], [0.2, 0.3])],
             [([0.0, 0.3], [0.6, 0.9]), ([0.5, 0.5], [0.5, 0.5]), ([0.6, 0.7], [0.2, 0.3]), ([0.6, 0.7], [0.1, 0.2])],
             [([0.2, 0.3], [0.6, 0.7]), ([0.2, 0.3], [0.6, 0.7]), ([0.5, 0.5], [0.5, 0.5]), ([0.6, 0.7], [0.1, 0.3])],
             [([0.2, 0.3], [0.6, 0.7]), ([0.1, 0.2], [0.6, 0.7]), ([0.1, 0.3], [0.6, 0.7]), ([0.5, 0.5], [0.5, 0.5])]],
            # prm_C1_e3
            [[([0.5, 0.5], [0.5, 0.5]), ([0.5, 0.6], [0.2, 0.3]), ([0.6, 0.7], [0.0, 0.3]), ([0.8, 0.9], [0.0, 0.1])],
             [([0.2, 0.3], [0.5, 0.6]), ([0.5, 0.5], [0.5, 0.5]), ([0.6, 0.7], [0.2, 0.3]), ([0.6, 0.7], [0.1, 0.2])],
             [([0.0, 0.3], [0.6, 0.7]), ([0.2, 0.3], [0.6, 0.7]), ([0.5, 0.5], [0.5, 0.5]), ([0.6, 0.7], [0.1, 0.3])],
             [([0.0, 0.1], [0.8, 0.9]), ([0.1, 0.2], [0.6, 0.7]), ([0.1, 0.3], [0.6, 0.7]), ([0.5, 0.5], [0.5, 0.5])]]],
            # prm_C1_e4
            [  # --c2
                [[([0.5, 0.5], [0.5, 0.5]), ([0.6, 0.7], [0.0, 0.3]), ([0.5, 0.7], [0.0, 0.1]),
                  ([0.5, 0.7], [0.1, 0.2])],
                 [([0.0, 0.3], [0.6, 0.7]), ([0.5, 0.5], [0.5, 0.5]), ([0.5, 0.8], [0.0, 0.1]),
                  ([0.6, 0.7], [0.1, 0.2])],
                 [([0.0, 0.1], [0.5, 0.7]), ([0.0, 0.1], [0.5, 0.8]), ([0.5, 0.5], [0.5, 0.5]),
                  ([0.7, 0.8], [0.0, 0.1])],
                 [([0.1, 0.2], [0.5, 0.7]), ([0.1, 0.2], [0.6, 0.7]), ([0.0, 0.1], [0.7, 0.8]),
                  ([0.5, 0.5], [0.5, 0.5])]],
                # prm_C1_e5
                [[([0.5, 0.5], [0.5, 0.5]), ([0.6, 0.9], [0.0, 0.1]), ([0.5, 0.9], [0.0, 0.1]),
                  ([0.7, 0.8], [0.1, 0.2])],
                 [([0.0, 0.1], [0.6, 0.9]), ([0.5, 0.5], [0.5, 0.5]), ([0.6, 0.8], [0.0, 0.1]),
                  ([0.6, 0.7], [0.1, 0.2])],
                 [([0.0, 0.1], [0.5, 0.9]), ([0.0, 0.1], [0.6, 0.8]), ([0.5, 0.5], [0.5, 0.5]),
                  ([0.7, 0.8], [0.0, 0.1])],
                 [([0.1, 0.2], [0.5, 0.8]), ([0.1, 0.2], [0.6, 0.7]), ([0.0, 0.1], [0.7, 0.8]),
                  ([0.5, 0.5], [0.5, 0.5])]],
                # prm_C1_e6
                [[([0.5, 0.5], [0.5, 0.5]), ([0.6, 0.7], [0.0, 0.3]), ([0.6, 0.7], [0.2, 0.3]),
                  ([0.5, 0.8], [0.1, 0.2])],
                 [([0.0, 0.3], [0.6, 0.7]), ([0.5, 0.5], [0.5, 0.5]), ([0.6, 0.8], [0.1, 0.2]),
                  ([0.6, 0.7], [0.1, 0.2])],
                 [([0.2, 0.3], [0.6, 0.7]), ([0.1, 0.2], [0.6, 0.8]), ([0.5, 0.5], [0.5, 0.5]),
                  ([0.5, 0.7], [0.1, 0.3])],
                 [([0.1, 0.2], [0.5, 0.8]), ([0.1, 0.2], [0.6, 0.7]), ([0.1, 0.3], [0.5, 0.7]),
                  ([0.5, 0.5], [0.5, 0.5])]]]
            # prm_C1_e7
        ],
        [[
            [[([0.5, 0.5], [0.5, 0.5]), ([0.1, 0.2], [0.7, 0.8]), ([0.4, 0.5], [0.2, 0.4])],
             [([0.7, 0.8], [0.1, 0.2]), ([0.5, 0.5], [0.5, 0.5]), ([0.7, 0.8], [0.1, 0.2])],
             [([0.2, 0.4], [0.4, 0.5]), ([0.1, 0.2], [0.7, 0.8]), ([0.5, 0.5], [0.5, 0.5])]],
            # prm_c2_e1
            [[([0.5, 0.5], [0.5, 0.5]), ([0.0, 0.2], [0.6, 0.7]), ([0.2, 0.3], [0.5, 0.7])],
             [([0.6, 0.7], [0.0, 0.2]), ([0.5, 0.5], [0.5, 0.5]), ([0.7, 0.9], [0.0, 0.1])],
             [([0.5, 0.7], [0.2, 0.3]), ([0.0, 0.1], [0.7, 0.9]), ([0.5, 0.5], [0.5, 0.5])]],
            # prm_c2_e2
            [[([0.5, 0.5], [0.5, 0.5]), ([0.1, 0.2], [0.7, 0.8]), ([0.3, 0.4], [0.5, 0.6])],
             [([0.7, 0.8], [0.1, 0.2]), ([0.5, 0.5], [0.5, 0.5]), ([0.7, 0.8], [0.0, 0.2])],
             [([0.5, 0.6], [0.3, 0.4]), ([0.0, 0.2], [0.7, 0.8]), ([0.5, 0.5], [0.5, 0.5])]],
            # prm_c2_e3
            [[([0.5, 0.5], [0.5, 0.5]), ([0.2, 0.3], [0.6, 0.7]), ([0.4, 0.5], [0.4, 0.5])],
             [([0.6, 0.7], [0.2, 0.3]), ([0.5, 0.5], [0.5, 0.5]), ([0.8, 0.9], [0.0, 0.1])],
             [([0.4, 0.5], [0.2, 0.3]), ([0.0, 0.1], [0.8, 0.9]), ([0.5, 0.5], [0.5, 0.5])]]
            # prm_c2_e4
        ],
            [
                [[([0.5, 0.5], [0.5, 0.5]), ([0.1, 0.2], [0.6, 0.7]), ([0.3, 0.4], [0.5, 0.6])],
                 [([0.6, 0.7], [0.1, 0.2]), ([0.5, 0.5], [0.5, 0.5]), ([0.6, 0.7], [0.0, 0.1])],
                 [([0.5, 0.6], [0.3, 0.4]), ([0.0, 0.1], [0.6, 0.7]), ([0.5, 0.5], [0.5, 0.5])]],
                # prm_c2_e5
                [[([0.5, 0.5], [0.5, 0.5]), ([0.2, 0.3], [0.5, 0.6]), ([0.4, 0.6], [0.3, 0.4])],
                 [([0.5, 0.6], [0.2, 0.3]), ([0.5, 0.5], [0.5, 0.5]), ([0.6, 0.7], [0.2, 0.3])],
                 [([0.3, 0.4], [0.4, 0.6]), ([0.2, 0.3], [0.6, 0.7]), ([0.5, 0.5], [0.5, 0.5])]],
                # prm_c2_e6
                [[([0.5, 0.5], [0.5, 0.5]), ([0.0, 0.2], [0.7, 0.8]), ([0.4, 0.5], [0.3, 0.5])],
                 [([0.7, 0.8], [0.0, 0.2]), ([0.5, 0.5], [0.5, 0.5]), ([0.7, 0.8], [0.1, 0.2])],
                 [([0.3, 0.5], [0.4, 0.5]), ([0.1, 0.2], [0.7, 0.8]), ([0.5, 0.5], [0.5, 0.5])]]
                # prm_c2_e7
            ]]], dtype=object)
    clusterData = np.array([
        [[([0.5, 0.5], [0.5, 0.5]), ([0.1, 0.2], [0.6, 0.7])],
         [([0.6, 0.7], [0.1, 0.2]), ([0.5, 0.5], [0.5, 0.5])]],
        [[([0.5, 0.5], [0.5, 0.5]), ([0.6, 0.7], [0.1, 0.3])],
         [([0.1, 0.3], [0.6, 0.7]), ([0.2, 0.3], [0.6, 0.7])]]
    ])
    q = 1
    anp = ANP(elementData, clusterData, q)
    result = anp.getResult()
    print(result)
